﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class RolRepository:MasterRepository,IRolRepository
    {
        public List<ROL> GetAllFromRol()
        {
            var query = from r in context.ROLs
                        select r;
            return query.ToList();
        }

        public List<ROL> GetAllFromRolByDescripcion(string descripcion)
        {
            var query = from r in context.ROLs
                        select r;
            if (descripcion != null && descripcion != string.Empty && descripcion != "")
            {
                query = from r in context.ROLs
                        where r.Descripcion.ToUpper().Contains(descripcion)
                        select r;
            }
            return query.ToList();
        }

        public ROL GetFromRolById(int id)
        {
            var query = from r in context.ROLs
                        where r.Id.Equals(id)
                        select r;
            return query.SingleOrDefault();
        }

        public bool ExisteDuplicidad(string descripcion, int id)
        {
            var query = from r in context.ROLs
                        where r.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 && r.Id != id
                        select r;
            return query.Any();
        }

        public void AddRol(ROL rol)
        {
            rol.USUARIOs = null;
            context.ROLs.Add(rol);
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void UpdateRol(ROL rol)
        {
            rol.USUARIOs = null;
            context.Entry(rol).State = System.Data.EntityState.Modified;
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void RemoveRol(int id)
        {
            var existe = context.ROLs.Find(id);

            if (existe != null)
            {
                context.ROLs.Remove(existe);
                context.SaveChanges();
            }
        }
    }
}
